import {
	createRouter,
	createWebHistory
} from 'vue-router'

const routes = [{
		path: '/home',
		name: 'home',
		component: () => import('../views/Home.vue'),
		children: [{
				path: '/home',
				redirect: "/home/welcome"
			},
			{
				path: 'welcome',
				name: 'welcome',
				component: () => import("../views/welcome.vue")
			},
			{
				path: 'homepage',
				name: 'homepage',
				component: () => import("../views/homepage.vue")
			},
			{
				path: 'adminuser',
				name: 'adminuser',
				component: () => import("../views/userviews/adminuser.vue")
			},
			{
				path: 'loweruser',
				name: 'loweruser',
				component: () => import("../views/userviews/loweruser.vue")
			},
			{
				path: 'admininfo',
				name: 'admininfo',
				component: () => import("../views/admininfo/admininfo.vue")
			}
		]
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/Login.vue')
	},
	{
		path: '/',
		redirect: '/login'
	},
]

const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	routes
})

router.beforeEach((to, from, next) => {
	if (to.path === '/login') return next()
	const token = localStorage.getItem('token')
	if (!token) return next('/login')
	next()
})

export default router